﻿
#include "zyScrollBar.h"

DarkMoonUI::zyScrollBar::zyScrollBar()
{
	SetMember(L"this", (LONG_PTR)this);
	m_obj->setWndProc(zyScrollBar_WndProc);
	m_obj->setChildWndProc(zyScrollBar_ChildWndProc);
	m_obj->setDestroy(zyScrollBar_Destroy);
	SetMember(L"LineNum", 1);
	背景色(GetSysColor(COLOR_BTNFACE));

}
DarkMoonUI::zyScrollBar::~zyScrollBar()
{

}
void DarkMoonUI::zyScrollBar::OnNCDestroy()
{
	HGDIOBJ BackBrush = (HGDIOBJ)GetMember(L"BackBrush");
	if (BackBrush != 0) {
		DeleteObject(BackBrush);
		SetMember(L"BackBrush", 0);
	}
}
void WINAPI DarkMoonUI::zyScrollBar::zyScrollBar_Destroy(zyObject* obj)
{
	zyScrollBar* zyb = (zyScrollBar*)obj->getPtrData(L"this");
	zyb->OnNCDestroy();
	zyCommandCtrol::zyCommand_Destroy(obj);
}
LRESULT WINAPI DarkMoonUI::zyScrollBar::zyScrollBar_ChildWndProc(zyObject* obj, HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, BOOL* bHandle)
{
	if (message == WM_HSCROLL || message == WM_VSCROLL) {
		SHORT nScrollCode = LOWORD(wParam);
		INT LineNum = (INT)(INT_PTR)obj->getPtrData(L"LineNum");
		HWND hwndScrollBar = (HWND)lParam;
		SCROLLINFO si = {0};
		si.cbSize = sizeof(SCROLLINFO);
		si.fMask = SIF_ALL;
		SendMessage(hwndScrollBar, SBM_GETSCROLLINFO, SB_CTL, (WPARAM)&si);
		INT nNewPos = si.nPos;
		if (nScrollCode == SB_LINEUP)
			nNewPos = nNewPos - LineNum;
		else if (nScrollCode == SB_LINEDOWN)
			nNewPos = nNewPos + LineNum;
		else if (nScrollCode == SB_PAGEUP)
			nNewPos = nNewPos - si.nPage;
		else if (nScrollCode == SB_PAGEDOWN)
			nNewPos = nNewPos + si.nPage;
		else if (nScrollCode == SB_THUMBTRACK)
			nNewPos = si.nTrackPos;
		else if (nScrollCode == SB_TOP)
			nNewPos = si.nMin;
		else if (nScrollCode == SB_BOTTOM)
			nNewPos = si.nMax;
		else if (nScrollCode == SB_ENDSCROLL)
			nNewPos = si.nPos;
		SendMessage(hwndScrollBar, SBM_SETPOS, nNewPos, 1);
		TRegEvent* evtPtr = (TRegEvent*)obj->getPtrData(L"OnPosChange");
		if (evtPtr != nullptr) {
			evtPtr->handler<LRESULT>(hwndScrollBar, nNewPos);
		}
		*bHandle = true;
		return 0;
	}
	else if (message >= WM_CTLCOLORMSGBOX && message <= WM_CTLCOLORSTATIC) {
		*bHandle = true;
		LONG_PTR BackBrush = (LONG_PTR)obj->getPtrData(L"BackBrush");
		SetBkColor((HDC)wParam, (COLORREF)(LONG_PTR)obj->getPtrData(L"BackColor"));
		return BackBrush;
	}
	return zyCommandCtrol::zyCommand_ChildWndProc(obj ,hWnd, message, wParam, lParam, bHandle);
}
LRESULT WINAPI DarkMoonUI::zyScrollBar::zyScrollBar_WndProc(zyObject* obj, HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL* bHandled)
{
	if (uMsg == WM_NCDESTROY) {
		zyScrollBar* zyb = (zyScrollBar*)obj->getPtrData(L"this");
		zyb->OnNCDestroy();
	}
	return zyCommandCtrol::zyCommand_WndProc(obj, hWnd, uMsg, wParam, lParam, bHandled);
}
void DarkMoonUI::zyScrollBar::_初始化滚动条() {
	SCROLLINFO sbi = {0};
	sbi.cbSize = sizeof(SCROLLINFO);
	sbi.fMask = SIF_RANGE | SIF_PAGE | SIF_POS | SIF_DISABLENOSCROLL;
	sbi.nMin = 0;
	sbi.nMax = 109;
	sbi.nPage = 10;
	sbi.nPos = 0;
	sendMsg(SBM_SETSCROLLINFO, SB_CTL, (LPARAM)&sbi);
}
BOOL DarkMoonUI::zyScrollBar::创建(HWND 参数_父窗口, const std::wstring& 参数_标题, POINT 参数_位置, POINT 参数_尺寸, int 参数_ID, int 参数_窗口风格, int 参数_扩展风格)
{
	if (参数_ID == NULL)
		参数_ID = 黑月默认ID();
	bool ret = _CreateCtrol(参数_父窗口, 参数_标题, 参数_位置, 参数_尺寸, 参数_ID, 参数_窗口风格 , 参数_扩展风格, L"ScrollBar");
	_初始化滚动条();
	return ret;
}

BOOL DarkMoonUI::zyScrollBar::创建(zyWindow& 参数_父窗口, const std::wstring& 参数_标题, POINT 参数_位置, POINT 参数_尺寸, int 参数_ID, int 参数_窗口风格, int 参数_扩展风格)
{
	if (参数_ID == NULL)
		参数_ID = 黑月默认ID();

	bool ret = _CreateCtrol(参数_父窗口.getHwnd(), 参数_标题, 参数_位置, 参数_尺寸, 参数_ID, 参数_窗口风格, 参数_扩展风格, L"ScrollBar");
	_初始化滚动条();
	return ret;
}
bool DarkMoonUI::zyScrollBar::绑定(HWND 参数_父窗口, int 参数_资源ID, bool 参数_使用默认字体) {
	HWND hwnd = GetDlgItem(参数_父窗口, 参数_资源ID);
	if (hwnd != 0) {
		_初始化滚动条();
		if (参数_使用默认字体)
			SendMessage(hwnd, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), 0);
	}
	return _BindWindow(hwnd);
}
bool DarkMoonUI::zyScrollBar::绑定窗口句柄(HWND 参数_窗口句柄) {
	if (IsWindow(参数_窗口句柄) != 0)
		_初始化滚动条();
	return _BindWindow(参数_窗口句柄);
}
INT DarkMoonUI::zyScrollBar::最小位置(INT pos) {
	SCROLLINFO si;
	si.cbSize = sizeof(SCROLLINFO);
	si.fMask = SIF_ALL;
	sendMsg(SBM_GETSCROLLINFO, SB_CTL, (LPARAM)&si);
	if (pos == NULL)
		return si.nMin;
	si.fMask = SIF_RANGE | SIF_DISABLENOSCROLL;
	si.nMin = pos;
	return (int)sendMsg(SBM_SETSCROLLINFO, SB_CTL, (LPARAM)&si);
}
INT DarkMoonUI::zyScrollBar::最大位置(INT pos) {
	SCROLLINFO si;
	si.cbSize = sizeof(SCROLLINFO);
	si.fMask = SIF_ALL;
	sendMsg(SBM_GETSCROLLINFO, SB_CTL, (LPARAM)&si);
	if (pos == NULL)
		return si.nMax;
	si.fMask = SIF_RANGE | SIF_DISABLENOSCROLL;
	si.nMax = pos;
	return (int)sendMsg(SBM_SETSCROLLINFO, SB_CTL, (LPARAM)&si);
}
INT DarkMoonUI::zyScrollBar::页改变值(INT value) {
	SCROLLINFO si;
	si.cbSize = sizeof(SCROLLINFO);
	si.fMask = SIF_ALL;
	sendMsg(SBM_GETSCROLLINFO, SB_CTL, (LPARAM)&si);
	if (value == NULL)
		return si.nPage;
	si.fMask = SIF_RANGE | SIF_DISABLENOSCROLL;
	si.nPage = value;
	return (int)sendMsg(SBM_SETSCROLLINFO, SB_CTL, (LPARAM)&si);
}
INT DarkMoonUI::zyScrollBar::行改变值(INT value) {
	if (value == NULL)
		return (INT)GetMember(L"LineNum");
	SetMember(L"LineNum", value);
	return 1;
}
int DarkMoonUI::zyScrollBar::位置(int 参数_位置) {
	SCROLLINFO si;
	si.cbSize = sizeof(SCROLLINFO);
	si.fMask = SIF_ALL;
	sendMsg(SBM_GETSCROLLINFO, SB_CTL, (LPARAM)&si);
	if (参数_位置 == -1)
		return si.nPos;
	si.fMask = SIF_POS | SIF_DISABLENOSCROLL;
	si.nPos = 参数_位置;
	return (int)sendMsg(SBM_SETSCROLLINFO, SB_CTL, (LPARAM)&si);
}